package cn.mvc.util;

import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 已确认全部可用
 *
 * @author yexi
 */
public class MediaTypeUtils {
    public static final Map<String, MediaType> MEDIA_TYPES = new HashMap<>();

    static {
        MEDIA_TYPES.put("pdf", MediaType.APPLICATION_PDF);
        MEDIA_TYPES.put("json", MediaType.APPLICATION_JSON);
        MEDIA_TYPES.put("gif", MediaType.IMAGE_GIF);
        MEDIA_TYPES.put("jpg", MediaType.IMAGE_JPEG);
        MEDIA_TYPES.put("jpeg", MediaType.IMAGE_JPEG);
        MEDIA_TYPES.put("png", MediaType.IMAGE_PNG);
        MEDIA_TYPES.put("txt", MediaType.TEXT_PLAIN);
        MEDIA_TYPES.put("xml", MediaType.TEXT_XML);
        MEDIA_TYPES.put("html", MediaType.TEXT_HTML);
        MEDIA_TYPES.put("htm", MediaType.TEXT_HTML);
        MEDIA_TYPES.put("md", MediaType.TEXT_MARKDOWN);
    }

    public static MediaType contentType(String fileName) {
        String extension = StringUtils.getFilenameExtension(fileName);
        return MEDIA_TYPES.getOrDefault(extension.toLowerCase(), MediaType.APPLICATION_OCTET_STREAM);
    }
}